package com.nwcl.common.utils;

import com.alibaba.fastjson.JSON;
import com.nwcl.common.session.SessionUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@SuppressWarnings("all")
@Component
public class YsxSessionUtil {
    private static String sessionKey = "ysx";
    private static int sessionTime = 24 * 30;// 单位小时
    @Autowired
    private RedisUtil redisUtil;

    public SessionUser getAccount(HttpServletRequest request) {
        String key = getCookie(request, SessionUser.class);
        String value = redisUtil.get(key);
        SessionUser session = JSON.parseObject(value, SessionUser.class);
        return session;
    }

    public void deleteSession(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        String value = "";
        if (cookies == null) {
            return;
        }
        for (Cookie c : cookies) {
            if (sessionKey.equals(c.getName())) {
                value = c.getValue();
                c.setValue(null);
                c.setMaxAge(0);
                c.setPath("/");
                response.addCookie(c);
                redisUtil.del(value);
            }
        }
    }

    public String getCookie(HttpServletRequest request, Class cla) {
        Cookie[] cookies = request.getCookies();
        String value = "";
        if (cookies == null) {
            return value;
        }

        for (Cookie c : cookies) {
            if (sessionKey.equals(c.getName())) {
                value = c.getValue();
                if (value == null) {
                    continue;
                }
                String a = redisUtil.get(value);
                if (a != null) {
                    break;
                }
            }
        }
        return value;
    }

    public  String addCookieAndRetKey(HttpServletResponse respone, SessionUser account ) {
        if(account == null){//表示session为空，不设置session cookie
            return null;
        }
        //设置cookie
        String key = redisUtil.get(sessionKey+account.getId());
        if(key==null){
            key = UUID.randomUUID().toString().replace("-", "");
            redisUtil.setex(sessionKey+account.getId(),key,YsxSessionUtil.sessionTime,TimeUnit.HOURS);
        }
        Cookie c2 = new Cookie(sessionKey, key);
        // 设置生命周期为1小时，秒为单位
        c2.setMaxAge(YsxSessionUtil.sessionTime*3600);
        c2.setPath("/");
        respone.addCookie(c2);
        redisUtil.setex(key,JSON.toJSONString(account),YsxSessionUtil.sessionTime,TimeUnit.HOURS);
        redisUtil.setex(sessionKey+account.getId(),key,YsxSessionUtil.sessionTime,TimeUnit.HOURS);
        return key;
    }

    public boolean updateSession(HttpServletRequest request) {
        String key = getCookie(request, SessionUser.class);
        String value = redisUtil.get(key);
        SessionUser user = JSON.parseObject(value, SessionUser.class);
        if (user == null) {
            return false;
        }
        user.setLastTime(new Date());
        redisUtil.set(key, JSON.toJSONString(user)); //修改session中的人员信息
        redisUtil.expire(key, YsxSessionUtil.sessionTime ,TimeUnit.HOURS);
        redisUtil.expire(sessionKey + user.getId(), YsxSessionUtil.sessionTime ,TimeUnit.HOURS);
        return true;
    }

    public static String getSessionKey() {
        return sessionKey;
    }

}
